using System;
using System.Collections.Generic;
using System.Text;

namespace MeetFuture.MRC
{
	public partial class clsRVMsg
	{
        private const string replaceString = "@@@@@@@@@@";
        private char[] messageSplitChar = " ".ToCharArray();
        private char[] bodySplitChar = "=".ToCharArray();
        private char[] valueSplitChar = ",".ToCharArray();
        private string _Header = null;
        private Dictionary<string, string> _MessageBody = new Dictionary<string, string>();
        private string _Message = string.Empty;

        public string Header
        {
            get { return this._Header; }
            set { this._Header = value; }
        }
        public string Message
        {
            get { return this._Message; }
            set { this._Message = value; }
        }
        public Dictionary<string, string> MessageBody
        {
            get { return this._MessageBody; }
        }

        public clsRVMsg(string msg)
        {
            try
            {
                msg = msg.Replace("\r", " ").Replace("\n", " ");
                this._Header = null;
                this._Message = msg;
                this._MessageBody = new Dictionary<string, string>();
                string[] thisMsg;
                if (msg.IndexOf("\"") >= 0)
                {
                    thisMsg = RemoveQuote(msg);
                }
                else
                {
                    thisMsg = msg.Split(messageSplitChar, StringSplitOptions.RemoveEmptyEntries);
                }                
                this._Header = thisMsg[0];
                foreach (string keyValue in thisMsg)
                {
                    try
                    {
                        string[] body = keyValue.Split(bodySplitChar);
                        switch (body.Length)
                        {
                            case 0:
                        	case 1:
                        	{                        
                        		break;
                        	}
                            case 2:
                            {
                                if (body[1].IndexOf(replaceString) >= 0)
                                {
                                    body[1] = body[1].Replace(replaceString, " ");
                                }
                                this._MessageBody.Add(body[0], body[1]);
                                break;
                            }
                        	default :
                        	{
                                string data = string.Empty;
                                for (int i = 1; i < body.Length; i++ )
                                {
                                    data += body[i] + "=";
                                }
                                if (data.IndexOf(replaceString) >= 0)
                                {
                                    data = data.Replace(replaceString, " ");
                                }
                                this._MessageBody.Add(body[0], data);
                        		break;
                        	}
                        }
                        if (body.Length > 1)
                        {
                            
                        }                        
                    }
                    catch (System.Exception ex)
                    {	
                    }                    
                }
            }
            catch (System.Exception ex)
            {	
            }
        }

        private string ReplaceQuote(string msg)
        {
            string result = msg;
            try
            {
                if (result.IndexOf("\"") >= 0)
            	{
                    string quoteMsg = msg.Substring(msg.IndexOf("\""), msg.IndexOf("\"", msg.IndexOf("\"") + 1) - msg.IndexOf("\"") + 1);
                    string tmpMsg = quoteMsg;
                    tmpMsg = tmpMsg.Replace(" ", replaceString).Replace("\"", string.Empty);
                    result = result.Replace(quoteMsg, tmpMsg);
                    if (result.IndexOf("\"") >= 0)
                    {
                        result = ReplaceQuote(result);
                    }
            	}
            }
            catch (System.Exception ex)
            {	
            }
            return result;
        }

        private string[] RemoveQuote(string msg)
        {
            string[] result = null;
            string[] AllMsg =null;
            string[] SubMsg = null;
            try
            {
                msg = ReplaceQuote(msg);
                result = msg.Split(messageSplitChar, StringSplitOptions.RemoveEmptyEntries);            
            }
            catch (System.Exception ex)
            {	
            }
            return result;
        }

        public string GetValueByKey(string key)
        {
            string result = string.Empty;
            try
            {
                this._MessageBody.TryGetValue(key, out result);
                if (result == null)
                {
                    result = string.Empty;
                }
            }
            catch (System.Exception ex)
            {
                result = string.Empty;
            }
            return result;
        }

        public string GetValueByKey(string key, int index)
        {
            string result = string.Empty;
            try
            {
                this._MessageBody.TryGetValue(key, out result);
                if (result != null)
                {
                    string[] splitValue = result.Split(valueSplitChar);
                    result = splitValue[index - 1];
                }
                else
                {
                    result = string.Empty;
                }
            }
            catch (System.Exception ex)
            {
                result = string.Empty;
            }
            return result;
        }

	}
}
